import ajax from 'axios';
import qs from 'qs';
const modules = {
  namespaced:true,
  state:{
    Banners:[],
    MusicList:[],
    MusicListDetail:{},
    tracks:{},
ListCom:{comment:[]},
    upComment:[
      {user:{avatarUrl:window.localStorage.getItem('head'),
        uid:window.localStorage.getItem('id'),
        nickName:window.localStorage.getItem('username')},
        likedCount:0}],
    faved: false,
isLike:false,
    topList:[],
    ArtistTopList1:[],
    ArtistTopList:[],
    ArtistDetail:{},
    ArtistDesc:[],
    ArtistSongs:[],
    ArtistAlbum:[],
    findMusic: {
      title: '发现好音乐',
      items: [
        {color: '#FFC46C'},
        {color: '#42BD56'},
        {color: '#4F9DED'},
        { color: '#CC3344'},
        {color: '#2384E8'},
        { color: '#FF4055'},
        { color: '#FFAC2D'},
        {color: '#4fce89'},
        {color: '#C779C6'},
        {color: '#F787Fd'}
      ]
    },
    musSearchResult:[],
    myLikeSongs:[],
    MusOldCom:[],
    MusCommentByUser:[],
    myLikeList:[],
    MusComList:[],
    MusSongsLike:[]
  },
  mutations:{
    musSearchResult(state,info){
      state.musSearchResult=info
    },
    MusicHotSearch(state,info){
      let obj= state.findMusic.items.map((item,index) => {
        return {...item, ...info[index]};
      });
      state.findMusic.items=obj.slice(0,10)
    },
    MusicBanner(state,info){
      state.Banners=info
    },
    MusicList(state,info){
      state.MusicList=info
    },
    MusicListById(state,info){
      state.MusicListDetail=info
      state.tracks=info.tracks
    },
    MusicListComById(state,info){
    state.ListCom.comment=info
      state.ListCom.comment=state.ListCom.comment.reverse()
    },
    MusAddComment(state,info){
      Object.assign(state.upComment[0],info)
    },
    MusFav(state,info){
      state.faved=info
    },
    MusLike(state,info){
      state.isLike=info
    },
    topList(state,info){
      state.topList=info
    },
    ArtistTopList1(state,info){
      state.ArtistTopList1=info
    },
    ArtistTopList(state,info){
      state.ArtistTopList=info
    },
    ArtistDetailById(state,info){
      state.ArtistDetail=info
    },
    ArtistDescById(state,info){
      state.ArtistDesc=info
    },
    ArtistSongs(state,info){
      state.ArtistSongs=info
    },
    ArtistAlbum(state,info){
      state.ArtistAlbum=info
    },
    MyLikeSongs(state,info){
      state.myLikeSongs=info
    },
    MyLikeList(state,info){
      state.myLikeList=info
    },
    MusOldCom(state,info){
      console.log(info)
      state.MusOldCom.push(info)
      sessionStorage["MusOldCom"] = JSON.stringify(state.MusOldCom);
    },
    MusCommentByUser(state,info){
      state.MusCommentByUser=info
    },
    getMusComList(state,info){
      state.MusComList=info
    },
    getMusSongLike(state,info){
      state.MusSongsLike=info
    }


  },
  actions:{
    musicSearch({commit},keyword){//音乐搜索
      ajax.get('/api3/search?keywords='+keyword).then((res)=>{
        commit('musSearchResult',res.data.result.songs);
      }).catch((error)=>{
        console.log(error)
      })
    },
    musicHotSearch({commit}){//音乐搜索推荐
      ajax.get('/api3/search/hot/detail').then((res)=>{
        commit('MusicHotSearch',res.data.data);
      }).catch((error)=>{
        console.log(error)
      })
    },
    musicBanner({commit}){//音乐轮播图
      ajax.get('/api3/banner').then((res)=>{
        commit('MusicBanner',res.data.banners);
      }).catch((error)=>{
        console.log(error)
      })
    },
    MusicList({commit}){//获取推荐歌单
      ajax.get('/api3/personalized').then((res)=>{
        commit('MusicList',res.data.result);
      }).catch((error)=>{
        console.log(error)
      })
    },
    MusicListById({commit},id){//歌单详情
      ajax.get('/api3/playlist/detail?id=' + id).then((res)=>{
        commit('MusicListById',res.data.playlist);
      }).catch((error)=>{
        console.log(error)
      })
    },
    MusicListComById({commit},id){//歌单评论
      let resCom=[]
      ajax.get('/api3/comment/playlist/detail?id=' + id).then((res)=>{
        res.data.hotComments?resCom=res.data.hotComments.concat(res.data.comments):resCom=res.data.comments
        ajax.get('/api/Comment/getCommentbyMovie?Id='+ id).then((res) => {
          res.data.data.map((item)=>{
            console.log(item)
            let con={...Object.assign(this.state.music.upComment[0],item)}
            resCom.push(con)
          })
          commit('MusicListComById',resCom);
        })
      }).catch((error)=>{
        console.log(error)
      })//本地歌单评论
    },
    MusAddComment({commit},addComment){//添加评论
      let allAdd=Object.assign(this.state.music.upComment[0],addComment)
      console.log(allAdd,addComment)
      if(addComment.uid){
        ajax.post('api/Comment/MusicListComment',allAdd).then((res)=>{
          commit('MusAddComment',res.data.data);
        }).catch((error)=>{console.log(error)})
      }else {
        commit('MusOldCom',addComment)
      }
    },
    musAddFav(context,playload){//添加喜欢
      ajax.post('api/music/addLike',qs.stringify(playload)).then((res)=>{
        context.commit('MusFav', res.data.data.fav);
      }).catch((error) => {
        console.log(error);
      });
    },
    musFav(context, param){//收藏状态
      ajax.post('/api/music/index',qs.stringify(param)).then((res) => {
        context.commit('MusLike', res.data.data.fav);
      }).catch((error) => {
        console.log(error);
      });
    },
    musAddLike(context,playload){//添加喜欢
      ajax.post('api/music/addLike',qs.stringify(playload)).then((res)=>{
        context.commit('MusLike', res.data.data.fav);
      }).catch((error) => {
        console.log(error);
      });
    },
    topList({commit}){//榜单
      ajax.get('/api3/toplist').then((res)=>{
        commit('topList',res.data.list);
      }).catch((error)=>{
        console.log(error)
      })
    },
    ArtistTopList({commit},playload){//歌手榜
      let url=''
      switch (playload){
        case '1':
          url='/api3/artist/list'
          break;
        case '2':
          url='/api3/artist/list?area=7'
          break;
        case '3':
          url='/api3/artist/list?area=96'
          break;
        case '4':
          url='/api3/artist/list?area=8'
          break;
        case '5':
          url='/api3/artist/list?area=16'
          break;
        case '6':
          url='/api3/artist/list?area=0'
          break;
      }
      ajax.get(url).then((res)=>{
       playload==='1'?commit('ArtistTopList1',res.data.artists):commit('ArtistTopList',res.data.artists);
      }).catch((error)=>{
        console.log(error)
      })
    },
    ArtistDetailById({commit},id){//歌手详情
      ajax.get('/api3/artist/detail?id=' + id).then((res)=>{
        commit('ArtistDetailById',res.data.data.artist);
      }).catch((error)=>{
        console.log(error)
      })
    },
    ArtistDescById({commit},id){//歌手详情
      ajax.get('/api3/artist/desc?id=' + id).then((res)=>{
        commit('ArtistDescById',res.data.introduction);
      }).catch((error)=>{
        console.log(error)
      })
    },
    ArtistSongs({commit},id){//歌手歌曲
      ajax.get('/api3/artist/top/song?id='+id).then((res)=>{
        commit('ArtistSongs',res.data.songs);
      }).catch((error)=>{
        console.log(error)
      })
    },
    ArtistAlbum({commit},id){//歌手专辑
      ajax.get('/api3/artist/album?id='+id+'&limit=30').then((res)=>{
        commit('ArtistAlbum',res.data.hotAlbums);
      }).catch((error)=>{
        console.log(error)
      })
    },
    MyLikeSongs({commit},uid){//用户喜欢音乐
      let arr=[]
      ajax.get('/api/music/getLikeSongsByUser?uid='+uid).then((res)=>{
        res.data.data.map((item,i)=>{
          ajax.get('/api3/song/detail?ids='+item.musicid).then((res)=>{
           arr.push(res.data.songs[0])
          }).catch((error)=>{
            console.log(error)
          })
        })
        commit('MyLikeSongs',arr);
      }).catch((error)=>{
        console.log(error)
      })
    },
    MyLikeLists({commit},uid){//用户歌单收藏
      let arrList=[]
      ajax.get('/api/music/getLikeListByUser?uid='+uid).then((res)=>{
        res.data.data.map((item,i)=>{
          ajax.get('/api3/playlist/detail?id='+item.musicid).then((res)=>{
            arrList.push(res.data.playlist)
          }).catch((error)=>{
            console.log(error)
          })
        })
        commit('MyLikeList',arrList);
      }).catch((error)=>{
        console.log(error)
      })
    },
    musLocalComByUser({commit},uid){//用户评论
      let Comarr=[]
      ajax.get('/api/comment/getMusCommentbyUser?uid='+uid).then((res)=>{
        res.data.data.map((item)=>{
          let con={...Object.assign(this.state.music.upComment[0],item)}
          Comarr.push(con)
        })
        commit('MusCommentByUser',Comarr);
      }).catch((error)=>{
        console.log(error)
      })
    },
    getMusComList({commit},query) {//管理员 用户评论
      if(!query){query=''}
      ajax.get('/api/Comment/getMusCommentList?query='+query).then((res) => {
        commit('getMusComList',res.data.data);
      }).catch((error) => {
        console.log(error);
      });

    },
    getMusSongLike({commit}){//管理员 音乐喜欢
      ajax.get('/api/Music/getSongsLike').then((res)=>{
        commit('getMusSongLike',res.data.data);
      }).catch((error)=>{
        console.log(error)
      })

    }

  }
}
export default modules
